/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.account;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.account.SysMenu;
import com.unis.model.account.SysUser;
import com.unis.service.account.SysMenuService;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-12-10
 */
@Controller
@RequestMapping("/sysmenu")
public class SysMenuController {

    private SysMenuService sysmenuService;

    /****************************************
     * Insert a instace of SysMenu
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertSysMenu(@RequestBody SysMenu sysmenu) throws Exception {
        sysmenu.setStatmentId("SysMenu.insert");
        sysmenuService.insert(sysmenu);
        Map<String, Object> resMap = new HashMap<String, Object>();
        resMap.put("msg", "添加菜单成功!");
        resMap.put("menu", sysmenu);
        return resMap;
    }

    /****************************************
     * Update a instace of SysMenu
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody List<LinkedHashMap<?, ?>> sysmenus) throws Exception {
        String status = "failure";
        sysmenuService.batchUpdate(sysmenus);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of SysMenu
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody SysMenu sysmenu) throws Exception {
        sysmenu.setStatmentId("SysMenu.delete");
        String status = "failure";
        sysmenuService.batchDelete(sysmenu);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of SysMenu
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        SysMenu sysmenu = new SysMenu();
        sysmenu.setStatmentId("SysMenu.queryAsList");
        BeanUtils.populate(sysmenu, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(sysmenu, pageMap);
        List<BaseModel> sysmenuList = sysmenuService.queryAsList(sysmenu);
        Map<String, Object> renderMap = GridUIMapper.render(sysmenu, sysmenuList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of SysMenu
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody SysMenu sysmenu) throws Exception {
        sysmenu.setStatmentId("SysMenu.queryAsObject");
        return sysmenuService.queryAsObject(sysmenu);
    }

    /****************************************
     * Query instaces of SysMenu
     ****************************************/
    @RequestMapping("/zTree")
    @ResponseBody
    public Object queryAsTree(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        SysMenu sysmenu = new SysMenu();
        sysmenu.setStatmentId("SysMenu.queryAsList");
        sysmenu.setPaging(false);
        BeanUtils.populate(sysmenu, paramMap);
        List<BaseModel> sysmenuList = sysmenuService.queryAsList(sysmenu);
        if (!paramMap.containsKey("menuid")) {
            SysMenu rootMenu = new SysMenu();
            rootMenu.setMenuid("0");
            rootMenu.setMenuname("系统菜单");
            rootMenu.setPid(null);
            rootMenu.setIsParent("true");
            sysmenuList.add(rootMenu);
        }
        return sysmenuList;
    }

    /****************************************
     * Query menus for login user
     ****************************************/
    @RequestMapping("/userMenus")
    @ResponseBody
    public Object queryMenus4LoginUser(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        SysUser sysUser = new SysUser();
        sysUser.setPaging(false);
        BeanUtils.populate(sysUser, paramMap);
        List<BaseModel> sysmenuList = sysmenuService.querySysUserMenus(sysUser);
        return sysmenuList;
    }

    @RequestMapping("/sysMenuMgr")
    public String sysMenuMgr() {
        return "sys/sysmenuList";
    }

    @Resource
    public void setSysMenuService(SysMenuService sysmenuService) {
        this.sysmenuService = sysmenuService;
    }
}
